Micron Document
`:top
In `F33f`_`[computer programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_programming]`_`f, a `!variable-length array`! (`!VLA`!), also called `!variable-sized`! or `!runtime-sized`!, is an `F33f`_`[array data structure`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Array_data_structure]`_`f whose length is determined at `F33f`_`[runtime`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Execution_(computing)]`_`f, instead of at `F33f`_`[compile time`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compile_time]`_`f.`:cite-ref-cray-1-0[`F5bf`_`[1`#cite-note-cray-1]`_`f] In the language `F33f`_`[C`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_(programming_language)]`_`f, the VLA is said to have a variably modified `F33f`_`[data type`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Data_type]`_`f that depends on a value (see `F33f`_`[Dependent type`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dependent_type]`_`f).

The main purpose of VLAs is to simplify programming of `F33f`_`[numerical algorithms`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Numerical_method]`_`f.

Programming languages that support VLAs include `F33f`_`[Ada`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ada_(programming_language)]`_`f, `F33f`_`[ALGOL 68`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ALGOL_68]`_`f (for non-flexible rows), `F33f`_`[APL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=APL_(programming_language)]`_`f, `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f (as unsafe-mode `F33f`_`[stack-allocated`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Stack-based_memory_allocation]`_`f arrays), `F33f`_`[COBOL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=COBOL]`_`f, `F33f`_`[Fortran`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Fortran]`_`f 90, `F33f`_`[J`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=J_(programming_language)]`_`f, and `F33f`_`[Object Pascal`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object_Pascal]`_`f (the language used in `F33f`_`[Delphi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Delphi_(software)]`_`f and `F33f`_`[Lazarus`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lazarus_(software)]`_`f, that uses FPC). `F33f`_`[C99`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C99]`_`f introduced support for VLAs, although they were subsequently relegated in `F33f`_`[C11`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C11_(C_standard_revision)]`_`f to a conditional feature, which implementations are not required to support;`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f] on some platforms, VLAs could be implemented formerly with `B100`F9d9`F33f`_`[alloca`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Alloca]`_`f()`f`b or similar functions.

Growable arrays (also called `F33f`_`[dynamic arrays`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dynamic_array]`_`f) are generally more useful than VLAs because dynamic arrays can do everything VLAs can do, and also support growing the array at run-time. For this reason, many programming languages (`F33f`_`[JavaScript`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaScript]`_`f, `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f, `F33f`_`[Python`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Python_(programming_language)]`_`f, `F33f`_`[R`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=R_(programming_language)]`_`f, etc.) only support growable arrays. Even in languages that support variable-length arrays, it's often recommended to avoid using (stack-based) variable-length arrays, and instead use (`F33f`_`[heap-based`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Heap_(data_structure)]`_`f) dynamic arrays.`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f]

>>Contents

• `F0af`_`[Memory`#memory]`_`f
• `F0af`_`[Allocation`#allocation]`_`f
• `F0af`_`[Implementation`#implementation]`_`f
• `F0af`_`[C99`#c99]`_`f
• `F0af`_`[Ada`#ada]`_`f
• `F0af`_`[Fortran 90`#fortran-90]`_`f
• `F0af`_`[Cobol`#cobol]`_`f
• `F0af`_`[C#`#c]`_`f
• `F0af`_`[Object Pascal`#object-pascal]`_`f
• `F0af`_`[References`#references]`_`f

-─

>>Memory

>>>Allocation

• The `F33f`_`[GNU Compiler Collection`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GNU_Compiler_Collection]`_`f (GCC) for C allocates memory for VLAs with `F33f`_`[automatic storage duration`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Automatic_variable]`_`f on the `F33f`_`[stack`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Stack-based_memory_allocation]`_`f.`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f] This is the faster and more straightforward option compared to heap-allocation, and is used by most compilers.
• VLAs can also be allocated on the `F33f`_`[heap`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory_management]`_`f and internally accessed using a pointer to this block.

>>Implementation

>>>C99

The following `F33f`_`[C99`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C99]`_`f function allocates a variable-length array of a specified size, fills it with floating-point values, and then passes it to another function for processing. Because the array is declared as an automatic variable, its lifetime ends when `B100`F9d9read_and_process()`f`b returns.

`B100`F9d9float read_and_process(int n)`f`b
`B100`F9d9{`f`b
`B100`F9d9 float vals[n];`f`b
`B100`F9d9`f`b
`B100`F9d9 for (int i = 0; i < n; ++i)`f`b
`B100`F9d9 vals[i] = read_val();`f`b
`B100`F9d9`f`b
`B100`F9d9 return process(n, vals);`f`b
`B100`F9d9}`f`b

In C99, the length parameter must come before the variable-length array parameter in function calls.`:cite-ref-cray-1-1[`F5bf`_`[1`#cite-note-cray-1]`_`f] In C11, a `B100`F9d9__STDC_NO_VLA__`f`b macro is defined if VLA is not supported.`:cite-ref-6[`F5bf`_`[6`#cite-note-6]`_`f] The C23 standard makes VLA types mandatory again. Only creation of VLA objects with automatic storage duration is optional.`:cite-ref-7[`F5bf`_`[7`#cite-note-7]`_`f] GCC had VLA as an extension before C99, one that also extends into its C++ dialect.

`F33f`_`[Linus Torvalds`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Linus_Torvalds]`_`f has expressed his displeasure in the past over VLA usage for arrays with predetermined small sizes because it generates lower quality assembly code.`:cite-ref-8[`F5bf`_`[8`#cite-note-8]`_`f] With the Linux 4.20 kernel, the `F33f`_`[Linux kernel`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Linux_kernel]`_`f is effectively VLA-free.`:cite-ref-9[`F5bf`_`[9`#cite-note-9]`_`f]

Although C11 does not explicitly name a size-limit for VLAs, some believe it should have the same maximum size as all other objects, i.e. SIZE_MAX bytes.`:cite-ref-10[`F5bf`_`[10`#cite-note-10]`_`f] However, this should be understood in the wider context of environment and platform limits, such as the typical stack-guard page size of 4 KiB, which is many orders of magnitude smaller than SIZE_MAX.

It is possible to have VLA object with dynamic storage by using a pointer to an array.

`B100`F9d9float read_and_process(int n)`f`b
`B100`F9d9{`f`b
`B100`F9d9 float (*vals)[n] = malloc(sizeof(float[n]));`f`b
`B100`F9d9`f`b
`B100`F9d9 for (int i = 0; i < n; ++i)`f`b
`B100`F9d9 (*vals)[i] = read_val();`f`b
`B100`F9d9`f`b
`B100`F9d9 float ret = process(n, *vals);`f`b
`B100`F9d9`f`b
`B100`F9d9 free(vals);`f`b
`B100`F9d9`f`b
`B100`F9d9 return ret;`f`b
`B100`F9d9}`f`b

>>>Ada

The following is the same example in `F33f`_`[Ada`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ada_(programming_language)]`_`f. Ada arrays carry their bounds with them, so there is no need to pass the length to the Process function.

`B100`F9d9type Vals_Type is array (Positive range <>) of Float;`f`b
`B100`F9d9`f`b
`B100`F9d9function Read_And_Process (N : Integer) return Float is`f`b
`B100`F9d9 Vals : Vals_Type (1 .. N);`f`b
`B100`F9d9begin`f`b
`B100`F9d9 for I in 1 .. N loop`f`b
`B100`F9d9 Vals (I) := Read_Val;`f`b
`B100`F9d9 end loop;`f`b
`B100`F9d9 return Process (Vals);`f`b
`B100`F9d9end Read_And_Process;`f`b

>>>Fortran 90

The equivalent `F33f`_`[Fortran 90`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Fortran]`_`f function is

`B100`F9d9function read_and_process(n) result(o)`f`b
`B100`F9d9 integer,intent(in)::n`f`b
`B100`F9d9 real::o`f`b
`B100`F9d9`f`b
`B100`F9d9 real,dimension(n)::vals`f`b
`B100`F9d9 integer::i`f`b
`B100`F9d9`f`b
`B100`F9d9 do i = 1,n`f`b
`B100`F9d9 vals(i) = read_val()`f`b
`B100`F9d9 end do`f`b
`B100`F9d9 o = process(vals)`f`b
`B100`F9d9end function read_and_process`f`b

when utilizing the Fortran 90 feature of checking procedure interfaces at compile time; on the other hand, if the functions use pre-Fortran 90 call interface, the (external) functions must first be declared, and the array length must be explicitly passed as an argument (as in C):

`B100`F9d9function read_and_process(n) result(o)`f`b
`B100`F9d9 integer,intent(in)::n`f`b
`B100`F9d9 real::o`f`b
`B100`F9d9`f`b
`B100`F9d9 real,dimension(n)::vals`f`b
`B100`F9d9 real::read_val, process`f`b
`B100`F9d9 integer::i`f`b
`B100`F9d9`f`b
`B100`F9d9 do i = 1,n`f`b
`B100`F9d9 vals(i) = read_val()`f`b
`B100`F9d9 end do`f`b
`B100`F9d9 o = process(vals,n)`f`b
`B100`F9d9end function read_and_process`f`b

>>>Cobol

The following `F33f`_`[COBOL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=COBOL]`_`f fragment declares a variable-length array of records `B100`F9d9DEPT-PERSON`f`b having a length (number of members) specified by the value of `B100`F9d9PEOPLE-CNT`f`b:

`B100`F9d9DATA DIVISION.`f`b
`B100`F9d9WORKING-STORAGE SECTION.`f`b
`B100`F9d901 DEPT-PEOPLE.`f`b
`B100`F9d9 05 PEOPLE-CNT PIC S9(4) BINARY.`f`b
`B100`F9d9 05 DEPT-PERSON OCCURS 0 TO 20 TIMES DEPENDING ON PEOPLE-CNT.`f`b
`B100`F9d9 10 PERSON-NAME PIC X(20).`f`b
`B100`F9d9 10 PERSON-WAGE PIC S9(7)V99 PACKED-DECIMAL.`f`b

The `F33f`_`[COBOL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=COBOL]`_`f VLA, unlike that of other languages mentioned here, is safe because COBOL requires specifying maximum array size. In this example, `B100`F9d9DEPT-PERSON`f`b cannot have more than 20 items, regardless of the value of `B100`F9d9PEOPLE-CNT`f`b.

>>>C#

The following `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f fragment declares a variable-length array of integers. Before C# version 7.2, a pointer to the array is required, requiring an "unsafe" context. The "unsafe" keyword requires an assembly containing this code to be marked as unsafe.

`B100`F9d9unsafe void DeclareStackBasedArrayUnsafe(int size)`f`b
`B100`F9d9{`f`b
`B100`F9d9 int* p = stackalloc int[size];`f`b
`B100`F9d9 p[0] = 123;`f`b
`B100`F9d9}`f`b

C# version 7.2 and later allow the array to be allocated without the "unsafe" keyword, through the use of the Span feature.`:cite-ref-11[`F5bf`_`[11`#cite-note-11]`_`f]

`B100`F9d9void DeclareStackBasedArraySafe(int size)`f`b
`B100`F9d9{`f`b
`B100`F9d9 Span<int> a = stackalloc int[size];`f`b
`B100`F9d9 a[0] = 123;`f`b
`B100`F9d9}`f`b

>>>Object Pascal

`F33f`_`[Object Pascal`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object_Pascal]`_`f dynamic arrays are allocated on the heap.`:cite-ref-12[`F5bf`_`[12`#cite-note-12]`_`f]

In this language, it is called a dynamic array. The declaration of such a variable is similar to the declaration of a static array, but without specifying its size. The size of the array is given at the time of its use.

`B100`F9d9program CreateDynamicArrayOfNumbers(Size: Integer);`f`b
`B100`F9d9var`f`b
`B100`F9d9 NumberArray: array of LongWord;`f`b
`B100`F9d9begin`f`b
`B100`F9d9 SetLength(NumberArray, Size);`f`b
`B100`F9d9 NumberArray[0] := 2020;`f`b
`B100`F9d9end.`f`b

Removing the contents of a dynamic array is done by assigning it a size of zero.

`B100`F9d9...`f`b
`B100`F9d9SetLength(NumberArray, 0);`f`b
`B100`F9d9...`f`b

>>References

`:cite-note-cray-1`!1.`! `F0af`_`[↑`#cite-ref-cray-1-0]`_`f "Variable Length Arrays". Archived from the original on 2018-01-26.
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "Variable Length – Using the GNU Compiler Collection (GCC)".
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f ISO 9899:2011 Programming Languages – C 6.7.6.2 4.
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f `:citerefraymond2000`aRaymond, Eric S. (2000). "Raymond Software Release Practice Howto: 6. Good development practice". `*The Linux Documentation Project`*.
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f "Code Gen Options - The GNU Fortran Compiler".
`:cite-note-6`!6.`! `F0af`_`[↑`#cite-ref-6]`_`f § 6.10.8.3 of the C11 standard (n1570.pdf)
`:cite-note-7`!7.`! `F0af`_`[↑`#cite-ref-7]`_`f § 6.10.9.3 of the C23 standard (n3054.pdf)
`:cite-note-8`!8.`! `F0af`_`[↑`#cite-ref-8]`_`f `:citereftorvalds2018`a`F33f`_`[Torvalds, Linus`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Linus_Torvalds]`_`f (7 March 2018). "LKML: Linus Torvalds: Re: VLA removal (was Re: [RFC 2/2] lustre: use VLA_SAFE)". `*Linux kernel`* (Mailing list).
`:cite-note-9`!9.`! `F0af`_`[↑`#cite-ref-9]`_`f "The Linux Kernel Is Now VLA-Free: A Win For Security, Less Overhead & Better For Clang - Phoronix". `*www.phoronix.com`*.
`:cite-note-10`!10.`! `F0af`_`[↑`#cite-ref-10]`_`f §6.5.3.4 and §7.20.3 of the C11 standard (n1570.pdf)
`:cite-note-11`!11.`! `F0af`_`[↑`#cite-ref-11]`_`f "stackalloc operator (C# reference)". Microsoft. 10 July 2024.
`:cite-note-12`!12.`! `F0af`_`[↑`#cite-ref-12]`_`f Michaël Van Canneyt. "Free Pascal Reference guide: Dynamic arrays".

`c`F0af`_`[↑ Back to top`#top]`_`f`a